home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / development kits / mac os / multiprocessing 2.1 sdk / sample code / cpu meter Ä / .c / preferences.c < prev   
Encoding:
C/C++ Source or Header  |  1999-11-29  |  4.1 KB  |  192 lines

  1. /*
  2. ********************************************************************************
  3. **
  4. ** File: Preferences.cp
  5. **
  6. ** Authors: Cary Farrier (CF)
  7. **
  8. ** Description:
  9. **
  10. **    Prefs file routines.
  11. **
  12. ********************************************************************************
  13. **
  14. ** Revision History
  15. **
  16. ** 17-Oct-96    CF    Created.
  17. ** 17-Nov-96    CF    Converted to Apple Anaglyph.
  18. ** 23-May-97    gw    Converted to Apple 3D SPEX.
  19. **
  20. ********************************************************************************
  21. */
  22. #include <Resources.h>
  23. #include <Folders.h>
  24. #include <Errors.h>
  25.  
  26. #include "Preferences.h"
  27.  
  28. /*
  29. ********************************************************************************
  30. ** static globals
  31. ********************************************************************************
  32. */
  33. static short gResFileRefnum, gOldResFileRefnum;
  34.  
  35. /*
  36. ********************************************************************************
  37. ** local functions
  38. ********************************************************************************
  39. */
  40. static Handle OpenPreferences(void);
  41. static void ClosePreferences(void);
  42.  
  43. /*
  44. ********************************************************************************
  45. **
  46. ** Name: ReadPreferences
  47. **
  48. ** Description:
  49. **
  50. **    Reads the prefs.
  51. **
  52. ********************************************************************************
  53. */
  54. OSStatus ReadPreferences(Preferences* outPrefs)
  55. {
  56.     Handle thePrefsHandle;
  57.  
  58.     // get the preferences
  59.     thePrefsHandle = OpenPreferences();
  60.     if (thePrefsHandle)
  61.     {
  62.         *outPrefs = *((Preferences *) * thePrefsHandle);
  63.         ClosePreferences();
  64.     }
  65.  
  66.     return noErr;
  67. }
  68.  
  69. /*
  70. ********************************************************************************
  71. **
  72. ** Name: WritePreferences
  73. **
  74. ** Description:
  75. **
  76. **    Writes the prefs.
  77. **
  78. ********************************************************************************
  79. */
  80. OSStatus WritePreferences(Preferences* outPrefs)
  81. {
  82.     Handle thePrefsHandle;
  83.  
  84.     // get the preferences
  85.     thePrefsHandle = OpenPreferences();
  86.     if (thePrefsHandle)
  87.     {
  88.         *((Preferences *) * thePrefsHandle) = *outPrefs;
  89.         ChangedResource(thePrefsHandle);
  90.         ClosePreferences();
  91.     }
  92.  
  93.     return noErr;
  94. }
  95.  
  96. /*
  97. ********************************************************************************
  98. **
  99. ** Name: OpenPreferences
  100. **
  101. ** Description:
  102. **
  103. **    Opens the preferences file and returns a handle to the
  104. **    preferences data structure.  If the prefs file doesn't exist, it is
  105. **    created.  If the prefs data doesn't exist it is created.
  106. **
  107. ********************************************************************************
  108. */
  109. static Handle OpenPreferences(void)
  110. {
  111.     short theVRefNum;
  112.     long theDirID;
  113.     OSStatus theError;
  114.     FSSpec theSpec;
  115.     Handle thePrefs;
  116.  
  117.     // save current res file
  118.     gOldResFileRefnum = CurResFile();
  119.  
  120.     // locate the Preferences folder    
  121.     theError = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &theVRefNum, &theDirID);
  122.     if (theError)
  123.         return NULL;
  124.  
  125.     // create the spec for the file
  126.     theError = FSMakeFSSpec(theVRefNum, theDirID, kPrefsName, &theSpec);
  127.     if (theError)
  128.     {
  129.         // create the res file if it doesn't exist
  130.         if (fnfErr == theError)
  131.         {
  132.             FSpCreateResFile(&theSpec, '????', kPrefsType, -1);
  133.             if (ResError())
  134.                 return NULL;
  135.         }
  136.         else
  137.             return NULL;
  138.     }
  139.  
  140.     // open the resource file
  141.     gResFileRefnum = FSpOpenResFile(&theSpec, fsRdWrPerm);
  142.     if ((-1 == gResFileRefnum) || ResError())
  143.     {
  144.         return NULL;
  145.     }
  146.  
  147.     // load the prefs resource
  148.     thePrefs = GetResource(kPrefsType, kPrefsID);
  149.     if (NULL == thePrefs)
  150.     {
  151.         // create it if it doesn't exist
  152.         thePrefs = NewHandleClear(sizeof(Preferences));
  153.         if (NULL == thePrefs)
  154.         {
  155.             CloseResFile(gResFileRefnum);
  156.             UseResFile(gOldResFileRefnum);
  157.             return NULL;
  158.         }
  159.  
  160.         AddResource(thePrefs, kPrefsType, kPrefsID, kPrefsName);
  161.         if (ResError())
  162.         {
  163.             CloseResFile(gResFileRefnum);
  164.             UseResFile(gOldResFileRefnum);
  165.             DisposeHandle(thePrefs);
  166.             return NULL;
  167.         }
  168.     }
  169.  
  170.     // done
  171.     return thePrefs;
  172. }
  173.  
  174. /*
  175. ********************************************************************************
  176. **
  177. ** Name: ClosePreferences
  178. **
  179. ** Description:
  180. **
  181. **    Closes the preferences file.
  182. **
  183. ********************************************************************************
  184. */
  185. static void ClosePreferences(void)
  186. {
  187.     CloseResFile(gResFileRefnum);
  188.     UseResFile(gOldResFileRefnum);
  189. }
  190.  
  191.  
  192.